import java.util.Random;
import java.util.Scanner;


<<<<<<< HEAD
public class PasswordManager {
//Hxl第一次共同处修改
=======


public class PasswordManager {
    //共同处修改 
>>>>>>> 43a843cd8294d2e4f967222fa3cdb6d8451a7108
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//创建一个用于读取用户的scanner对象


        while (true) {//进入循环直到用户选择退出
            System.out.println("请选择操作:");//提示用户选择操作
            System.out.println("1. 加密");
            System.out.println("2. 解密");
            System.out.println("3. 判断密码强度");
            System.out.println("4. 生成密码");
            System.out.println("5. 退出");
            int choice = scanner.nextInt();//读取用户输入的选择

            switch (choice) {
                case 1:
                    System.out.print("请输入要加密的字符串: ");
                    scanner.nextLine();
                    String input = scanner.nextLine();// 读取用户输入的要加密的字符串
                    String encrypted = encrypt(input);// 调用 encrypt 方法加密字符串
                    System.out.println("加密后的字符串: " + encrypted);
                    break;
                case 2:
                    System.out.print("请输入要解密的字符串: ");
                    scanner.nextLine();
                    String encryptedInput = scanner.nextLine();// 读取用户输入的要解密的字符串
                    String decrypted = decrypt(encryptedInput);// 调用 decrypt 方法解密字符串
                    System.out.println("解密后的字符串: " + decrypted);
                    break;
                case 3:
                    System.out.print("请输入要检查强度的密码: ");
                    scanner.nextLine();// 读取用户输入的要检查强度的密码
                    String password = scanner.nextLine();
                    String strength = checkPasswordStrength(password);// 调用 checkPasswordStrength 方法检查密码强度
                    System.out.println("密码强度: " + strength);
                    break;
                case 4:
                    int passwordLength = 8;  // 可根据需要更改密码长度
                    String generatedPassword = generatePassword(passwordLength);// 调用 generatePassword 方法生成指定长度的密码
                    System.out.println("生成的密码: " + generatedPassword);
                    break;
                case 5:
                    System.out.println("退出程序");
                    System.exit(0);
                default:
                    System.out.println("无效的选择");
            }
        }
    }

    private static String encrypt(String input) {
        StringBuilder encrypted = new StringBuilder();
        int offset = 3;

        for (int i = 0; i < input.length(); i++) {
            char originalChar = input.charAt(i);
            int asciiValue = (int) originalChar;

            int encryptedAsciiValue = asciiValue + i + offset;

            char encryptedChar = (char) encryptedAsciiValue;
            encrypted.append(encryptedChar);
        }

        if (encrypted.length() >= 2) {
            char firstChar = encrypted.charAt(0);
            char lastChar = encrypted.charAt(encrypted.length() - 1);
            encrypted.setCharAt(0, lastChar);
            encrypted.setCharAt(encrypted.length() - 1, firstChar);
        }

        encrypted.reverse();

        return encrypted.toString();
    }

    private static String decrypt(String encryptedInput) {
        StringBuilder decrypted = new StringBuilder(encryptedInput).reverse();

        if (decrypted.length() >= 2) {
            char firstChar = decrypted.charAt(0);
            char lastChar = decrypted.charAt(decrypted.length() - 1);
            decrypted.setCharAt(0, lastChar);
            decrypted.setCharAt(decrypted.length() - 1, firstChar);
        }

        int offset = 3;

        for (int i = 0; i < decrypted.length(); i++) {
            char decryptedChar = decrypted.charAt(i);
            int asciiValue = (int) decryptedChar;

            int originalAsciiValue = asciiValue - i - offset;

            char originalChar = (char) originalAsciiValue;
            decrypted.setCharAt(i, originalChar);
        }

        return decrypted.toString();
    }

    private static String checkPasswordStrength(String password) {
        // 简单示例：根据长度来判断密码强度
        if (password.length() < 8) {
            return "弱";
        } else if (password.length() < 12) {
            return "中";
        } else {
            return "强";
        }
    }

    private static String generatePassword(int length) {
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder generatedPassword = new StringBuilder();

        Random random = new Random();

        for (int i = 0; i < length; i++) {
            char randomChar = characters.charAt(random.nextInt(characters.length()));
            generatedPassword.append(randomChar);
        }

        return generatedPassword.toString();
    }
}
